/**
 * 缓存说明:虚拟设备缓存
 */
import vm from '@/cache/storeTools.js'
import deviceApi from '@/components/api/deviceApi.js'
import menuApi from '@/components/api/menuApi.js'
import userApi from '@/components/api/userApi.js'
import roleApi from '@/components/api/roleApi.js'
import {getAllRightNew} from "../../api/abbNewApi";

var basestate = {
	// 当前公司信息
	nowUnit: {},
	// 当前用户信息
	nowUser: {},
	// 当前公司所有权限
	allRightDatas: {appRight:[],menuRight:[],pcmDefault:[],miniRight:[]},
	// 当前用户的web菜单权限ids数组
	nowUserMenuRightIds: [],
	// 当前用户app菜单权限ids数组
	nowUserAppRightIds: [],
	// 当前用户mini菜单权限ids数组
	nowUserMiniRightIds: [],
	// 当前用户web菜单不可编辑项
	editDisableMenuList: [],
	// 当前用户App菜单不可编辑项
	editDisableAppList: [],
	// 当前用户Mini菜单不可编辑项
	editDisableMiniList: [],
	// 当前公司设备树
	nowUnitDeviceTree: {},
	// 当前用户设备对象
	nowUserDeviceDatas: {view:[],edit:[]},
	// 当前用户可查看设备ids数组
	nowUserViewDeviceIds: [],
	// 当前用户可编辑设备ids数组
	nowUserEditDeviceIds: [],
	// 用户列表
	userList: [],
	// 当前用户绑定的角色ids数组
	nowUserRoleIds: [],
	// 所有系统角色
	allRoles: [],
	// 当前用户绑定角色的权限
	nowUserRolesRights: {pcmDefault:[],appRight:[],menuRight:[],miniRight:[]},
	//点击到报表模块就屏蔽掉全局数组方法
	shieldArray:true
}

var store = new vm.Vuex.Store({
	state: copy(basestate),
	getters: {
		// 获取所有用户权限
		getAllRightDatas(state, getters) {
			if(isempty(state.allRightDatas)){
				store.commit('getAllRight',state.nowUnit.id)
				return []
			}
			return state.allRightDatas
		},
		// 获取单位设备树
		getVirtualDeviceTreeByUnitId(state, getters) {
			if(state.nowUnitDeviceTree == {}){
				store.commit('getVirtualDeviceTreeByUnitId', state.nowUnit.id)
				return {}
			}
			return state.nowUnitDeviceTree
		},
		// 获取用户查看设备ids数组
		getNowUserViewDeviceIds(state, getters) {
			if(isempty(state.nowUserViewDeviceIds)){
				store.commit('getDeviceNosByUserId', state.nowUser.id)
				return []
			}
			return state.nowUserViewDeviceIds
		},
		// 获取用户编辑设备ids数组
		getNowUserViewDeviceIds(state, getters) {
			if(isempty(state.nowUserEditDeviceIds)){
				store.commit('getDeviceNosByUserId', state.nowUser.id)
				return []
			}
			return state.nowUserEditDeviceIds
		},
		getUserList(state, getters) {
			if(isempty(state.userList)){
				store.commit('getUsersByUnitId', state.nowUnit.id)
				return []
			}
			return state.userList
		},
		getUserRoleIds(state, getters) {
			if(isempty(state.nowUserRoleIds)){
				store.commit('getRoleIdsByUserId', state.nowUser.id)
				return []
			}
			return state.nowUserRoleIds
		},
		getAllRoles(state, getters) {
			if(isempty(state.allRoles)){
				store.commit('getAllRoles')
				return []
			}
			return state.allRoles
		}
	},
	mutations: {
		clear() {
			//清理缓存
			store.replaceState(copy(basestate))
		},
		setshieldArray(state,type){
			state.shieldArray = type
		},
		setUnit(state, unitData) {
			state.nowUnit = unitData
			store.commit('getAllRight', unitData.id)
			store.commit('getAllRoles', unitData.id)
			store.commit('getUsersByUnitId', unitData.id)
			store.commit('getVirtualDeviceTreeByUnitId', unitData.id)
		},
		setUser(state, userInfo) {
			state.nowUser = userInfo
			store.commit('getRightByUserId', userInfo.id)
			store.commit('getRoleIdsByUserId', userInfo.id)
			store.commit('getDeviceNosByUserId', userInfo.id)
		},
		getAllRight(state, unitId) {
			state.allRightDatas = {appRight:[],menuRight:[],pcmDefault:[]}
			if(unitId == undefined) {
				return 
			}
			function addName(arr){
				for(let i = 0;i < arr.length;i++){
					arr[i].name = arr[i].rightname
					if(!isempty(arr[i].next)){
						addName(arr[i].next)
					}
				}
			}
			menuApi.getAllRight(unitId, res => {
				addName(res.menuRight)
				addName(res.appRight)
				addName(res.miniRight)
				state.allRightDatas = res
			})
		},
		getAllRightCopy(state,unitId){
			function addName(arr){
				for(let i = 0;i < arr.length;i++){
					arr[i].name = arr[i].rightname
					if(!isempty(arr[i].next)){
						addName(arr[i].next)
					}
				}
			}
			getAllRightNew({'unitId':unitId}).then(res => {
				addName(res.data.menuRight)
				addName(res.data.appRight)
				addName(res.data.miniRight)
				state.allRightDatas = res.data
			})
			// getAllRightNew(unitId, res => {
			// 	state.allRightDatas = res
			
			// })
		},

		getRightByUserId(state, userId) {
			state.nowUserMenuRightIds = []
			state.nowUserAppRightIds = []
			state.nowUserMiniRightIds = []
			state.editDisableMenuList = []
			state.editDisableAppList = []
			state.editDisableMiniList = []
			if(userId == undefined){
				return
			}
			menuApi.getRightByUserId(state.nowUser.id, res => {
				// 设置选中项,不可编辑项
				var arrApp = []
				var editDisableAppList = []
				for(let i = 0;i < res.appRight.length;i++){
					arrApp.push(res.appRight[i].id)
					if(!res.appRight[i].editable){
						editDisableAppList.push(res.appRight[i].id)
					}
				}
				var arrMenu = []
				var editDisableMenuList = []
				for(let i = 0;i < res.menuRight.length;i++){
					arrMenu.push(res.menuRight[i].id)
					if(!res.menuRight[i].editable){
						editDisableMenuList.push(res.menuRight[i].id)
					}
				}

				var arrMini = []
				var editDisableMiniList = []
				for(let i = 0;i < res.miniRight.length;i++){
					arrMini.push(res.miniRight[i].id)
					if(!res.miniRight[i].editable){
						editDisableMiniList.push(res.miniRight[i].id)
					}
				}
				state.nowUserMenuRightIds = arrMenu
				state.nowUserAppRightIds = arrApp
				state.nowUserMiniRightIds = arrMini
				state.editDisableMenuList = editDisableMenuList
				state.editDisableAppList = editDisableAppList
				state.editDisableMiniList = editDisableMiniList
			})
		},
		getVirtualDeviceTreeByUnitId(state, unitId) {
			state.nowUnitDeviceTree = {}
			if(unitId == undefined){
				return
			}
			deviceApi.getVirtualDeviceTreeByUnitId(unitId, res => {
				state.nowUnitDeviceTree = res
			})
		},
		// 获取当前用户的设备列表
		getDeviceNosByUserId(state, userId) {
			state.nowUserDeviceDatas = {view:[],edit:[]},
			state.nowUserViewDeviceIds = []
			state.nowUserEditDeviceIds = []
			if(userId == undefined){
				return
			}
			deviceApi.getDeviceNosByUserId(userId, res => {
				state.nowUserDeviceDatas = res
				var arrView = []
				for(let i = 0;i < res.view.length;i++){
					arrView.push(res.view[i].deviceno)
				}
				var arrEdit = []
				for(let i = 0;i < res.edit.length;i++){
					arrEdit.push(res.edit[i].deviceno)
				}
				state.nowUserViewDeviceIds = arrView
				state.nowUserEditDeviceIds = arrEdit
			})
		},
		getUsersByUnitId(state, unitId) {
			state.userList = []
			if(unitId == undefined){
				return
			}
			userApi.findUserByUnitId(unitId, res=>{
				state.userList = res
				if(res.length > 0){
					store.commit('setUser', res[0])
				}else{
					store.commit('setUser', {})
				}
			})
		},
		//获取搜索用户名出来的数据
		getSearchData(state,data){
            state.userList = [data]
		},
		getScreenData(state,data){
            state.userList = data
		},
		getRoleIdsByUserId(state, userId) {
			state.nowUserRoleIds = []
			if(userId == undefined){
				return
			}
			roleApi.getRoleIdsByUserId(userId, res => {
				state.nowUserRoleIds = res
			})
		},
		getAllRoles(state) {
			state.allRoles = []
			roleApi.getAllRole(res => {
				state.allRoles = res
			})
		},
		getRightsByRoleIds(state, roleIds) {
			state.nowUserRolesRights = {pcmDefault:[],appRight:[],menuRight:[],miniRight:[]}
			menuApi.getRightsByRoleIds(roleIds.join(','), res => {
				state.nowUserRolesRights = res
			})
		},
	}
});

//对外提供
export default store